<?php

namespace app\api\controller\v1;

use app\admin\model\Channelbank;
use app\admin\model\Userbanklog;
use app\admin\model\Userverify;
use app\admin\model\Userweixin;
use app\api\service\BankService;
use app\api\service\CommonService;
use app\api\service\fYPayCardservice;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\User;
use EasyWeChat\Factory;
use fast\Random;
use think\Validate;

/**
 * 用户实名
 */
class Userbank extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $app = null;

    public function _initialize()
    {
        parent::_initialize();

        $config = [
            'app_id' => 'wx24f19287ae7f5d5e',
            'secret' => '4d76532cde1d11b0e03054083eddea4b',

            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            'log' => [
                'level' => 'debug',
            ],
        ];
        $this->app = Factory::miniProgram($config);
    }


    /**
     * 银行卡添加
     */
    public function add()
    {


        $bankNum = $this->request->param('bankNum');
        $bankPhone = $this->request->param('bankPhone');

        if (!$bankNum) {
            $this->error("参数错误");
        }
        $bankNum = str_replace(' ', '', $bankNum);

        $bankService = new BankService();
        $ret = $bankService->checkBank($bankNum);
        $data = json_decode($ret, true);
        $stat = $data['stat'];
        $bankName = "";
        $bankCode = "";

        if ($stat == 'ok') {
            $validated = $data['validated'];
            if (!$validated) {
                $this->error("卡号校验错误,请检查一遍");
            }
            $bankCode = $data['bank'];
            $bankName = json_decode($bankService->banks, true)[$bankCode];
        } else {
            $this->error("卡号错误,请检查一遍");

        }
        $uid = $this->auth->getUser()['id'];
        $insertData = array();
        $insertData['uid'] = $uid;
        $insertData['bankName'] = $bankName;
        $insertData['bankCode'] = $bankCode;
        $insertData['bankIcon'] = "https://yangfubing.gitee.io/bank.logo/resource/logo/" . $bankCode . ".png";
        $insertData['bankNum'] = $bankNum;
        $insertData['createtime'] = time();
        $insertData['bankPhone'] = $bankPhone;
        $userBankModel = new \app\admin\model\Userbank();

        $userbank = $userBankModel->where('uid', $uid)->find();
        if ($userbank) {
            $this->error("结算银行卡已存在，请勿重复添加");
        }

        $userVerifyModel = new Userverify();
        $userVerify = $userVerifyModel->where('uid', $uid)->find();
        $commonService = new CommonService();

        $obj = json_decode($ret, true);
        $status = '01';
        $msg = '01';
        if ($status == '01') {
            $id = $userBankModel->insert($insertData);
            $this->success("添加成功");
        } else {
            $msg = "";
            if ($status == '02') {
                $msg = "验证不通过【身份证或姓名手机与卡号不符】";
            } else if ($status == '202') {
                $msg = "银行卡无法验证";
            } else if ($status == '203') {
                $msg = "异常情况";
            } else if ($status == '204') {
                $msg = "姓名输入错误";

            } else if ($status == '205') {
                $msg = "身份证号输入错误";
            } else if ($status == '206') {
                $msg = "银行卡号输入错误";
            } else {
                $msg = "电话号码输入错误";
            }
            $this->error($msg);

        }


    }

    /**
     * 更换银行卡
     */
    public function update()
    {

        $id = $this->request->param('id');
        $bankNum = $this->request->param('bankNum');
        $bankPhone = $this->request->param('bankPhone');


        if (!$id || !$bankNum) {
            $this->error("参数错误");
        }

        $bankNum = str_replace(' ', '', $bankNum);
        $bankService = new BankService();
        $ret = $bankService->checkBank($bankNum);
        $data = json_decode($ret, true);
        $stat = $data['stat'];
        $bankName = "";
        $bankCode = "";

        if ($stat == 'ok') {
            $validated = $data['validated'];
            if (!$validated) {
                $this->error("卡号校验错误,请检查一遍");
            }
            $bankCode = $data['bank'];
            $bankName = json_decode($bankService->banks, true)[$bankCode];
        } else {
            $this->error("卡号错误,请检查一遍");

        }

        $uid = $this->auth->getUser()['id'];
        $insertData = array();
        $insertData['uid'] = $uid;
        $insertData['bankName'] = $bankName;
        $insertData['bankCode'] = $bankCode;
        $insertData['bankIcon'] = "https://yangfubing.gitee.io/bank.logo/resource/logo/" . $bankCode . ".png";
        $insertData['bankNum'] = $bankNum;
        $insertData['createtime'] = time();
        $insertData['bankPhone'] = $bankPhone;


        $userVerifyModel = new Userverify();
        $userVerify = $userVerifyModel->where('uid', $uid)->find();
        $commonService = new CommonService();


        $ret = $commonService->bankVerify($userVerify['idcard'], $userVerify['username'], $bankPhone, $bankNum);
        $obj = json_decode($ret, true);
        $status = $obj['status'];
        $msg = $obj['msg'];
        if ($status == '01') {

            $userBankModel = new \app\admin\model\Userbank();
            $id = $userBankModel->where('id', $id)->update($insertData);

            $this->success("更新成功");
        } else {
            $msg = "";
            if ($status == '02') {
                $msg = "验证不通过【身份证或姓名手机与卡号不符】";
            } else if ($status == '202') {
                $msg = "银行卡无法验证";
            } else if ($status == '203') {
                $msg = "异常情况";
            } else if ($status == '204') {
                $msg = "姓名输入错误";

            } else if ($status == '205') {
                $msg = "身份证号输入错误";
            } else if ($status == '206') {
                $msg = "银行卡号输入错误";
            } else {
                $msg = "电话号码输入错误";
            }

            $this->error($msg,'');
        }


    }

    /**
     * 获取用户银行卡信息
     */
    public
    function info()
    {
        $uid = $this->auth->getUser()['id'];
        $userBankModel = new \app\admin\model\Userbank();
        $bank = $userBankModel->where('uid', $uid)->find();
        $verifyModel = new Userverify();
        if ($bank) {
            $userVerify = $verifyModel->where('uid', $uid)->find();
            $bank['username'] = $userVerify['username'];
        }
        $this->success('', $bank);
    }


}
